package com.lge.clock.worldclock.map;

import javax.microedition.khronos.opengles.GL10;

import com.lge.clock.worldclock.util.DataManager;
import com.lge.clock.worldclock.util.GLDataObj;

/**
 * @author lge.com
 */
public class EarthSphere extends Shape{
	@SuppressWarnings("unused")
	private static final String PREFIX = "EarthSphere";
	
	private float mVertices[] = null;
	private float mTextures[] = null;
	private float mNormals[] = null;
	private GLDataObj glObj = null;
	
	private boolean mVisible = true; 
	
	public EarthSphere(String glObjName) {
		initLoadData(glObjName);
		initProcess();
		
		if (mVertices != null) mVertexSize = mVertices.length / 3;
	}
	
	private void initLoadData(String glObjName) {
		glObj = DataManager.getGlObj(glObjName);
		if(glObj != null) {
			mVertices = glObj.getVertices();
			mTextures = glObj.getTextures();
			mNormals = glObj.getNormals();
		}
	}
	
	private void initProcess() {
		if (mVertices != null) {
		    setVertexBuffer(mVertices);
		}
		
		if (mTextures != null) {
		    setTextureBuffer(mTextures);
		}
		
		if (mNormals != null) {
		    setNormalBuffer(mNormals);
		}
	}
	
	public void setVisible(boolean visible) {
		mVisible = visible;
	}
	
	@Override
	public void draw(GL10 gl) {
		
		if(!mVisible) {
			return;
		}
		
		if (isTranslateProcess() || isRotateProcess() || isScaleProcess()) {
			gl.glLoadIdentity();
			
			if (isTranslateProcess()) doTranslatef(gl);
			
			if (isRotateProcess()) doRotatef(gl);
			
			doInitRotate(gl);
			
			if (isScaleProcess()) doScalef(gl);
		}
		
		// Counter-clockwise winding.
		gl.glFrontFace(GL10.GL_CCW);
		// Enable face culling.
		gl.glEnable(GL10.GL_CULL_FACE);
		// What faces to remove with the face culling.
		gl.glCullFace(GL10.GL_BACK);
		
		// use Texturing
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mBindTextures[0]);
		
		// Enabled the vertices buffer for writing and to be used during
		// rendering.
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		// Specifies the location and data format of an array of vertex
		// coordinates to use when rendering.
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		
		if (mNormalBuffer != null) {
			gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
			gl.glNormalPointer(GL10.GL_FLOAT, 0, mNormalBuffer);
		}
		
		if (mTextureBuffer != null && mIsTextureProcess) {
			// color set, texture set - multiplication.
			gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
		} else {
			// Set flat color
			gl.glColor4f(mRGBAValue[0], mRGBAValue[1], mRGBAValue[2], mRGBAValue[3]);
			// Smooth color
			if (mColorBuffer != null && mIsColorsProcess) {
				// Enable the color array buffer to be used during rendering.
				gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
				// Point out the where the color buffer is.
				gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
			}
		}
		
		if (mIndexBuffer != null) {
			gl.glDrawElements(mRenderMode, mIndicesSize,
					GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
		} else {
			gl.glDrawArrays(mRenderMode, 0, mVertexSize);
		}
		
		// Disable the vertices buffer.
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
		if (mNormalBuffer != null) {
			gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
		}
		
		if (mTextureBuffer != null && mIsTextureProcess) {
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		} 
		else {
			if (mColorBuffer != null && mIsColorsProcess) {
				// Disable the colors buffer.
				gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
			}
		}
		//Disable Texturing
		gl.glDisable(GL10.GL_TEXTURE_2D);
		
		// Disable face culling.
		gl.glDisable(GL10.GL_CULL_FACE);
	}
}
